<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>通知配置管理 - Prometheus 管理器</title>
    <link href="/static/bootstrap.min.css" rel="stylesheet">
    <link href="/static/bootstrap-icons.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="/">Prometheus 管理器</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/collectors">采集器</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/alert_rules">告警规则</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/notifications">通知配置</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/alert_history">告警历史</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>通知配置管理</h2>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addNotificationModal">
                <i class="bi bi-plus-circle"></i> 添加通知配置
            </button>
        </div>

        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>名称</th>
                        <th>类型</th>
                        <th>配置信息</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for config in notification_configs %}
                    <tr>
                        <td>{{ config.id }}</td>
                        <td>{{ config.name }}</td>
                        <td>
                            {% if config.type == 'email' %}
                                <span class="badge bg-primary">Email</span>
                            {% elif config.type == 'dingtalk' %}
                                <span class="badge bg-info">钉钉</span>
                            {% elif config.type == 'feishu' %}
                                <span class="badge bg-warning">飞书</span>
                            {% elif config.type == 'wechat' %}
                                <span class="badge bg-success">企业微信</span>
                            {% endif %}
                        </td>
                        <td>
                            <small>
                                {% for key, value in config.config.items() %}
                                    {{ key }}: {{ value }}{% if not loop.last %}, {% endif %}
                                {% endfor %}
                            </small>
                        </td>
                        <td>
                            {% if config.enabled %}
                                <span class="badge bg-success">启用</span>
                            {% else %}
                                <span class="badge bg-secondary">禁用</span>
                            {% endif %}
                        </td>
                        <td>
                            <button class="btn btn-sm btn-outline-primary" onclick="editNotification({{ config.id }})">
                                <i class="bi bi-pencil"></i>
                            </button>
                            <button class="btn btn-sm btn-outline-danger" onclick="deleteNotification({{ config.id }})">
                                <i class="bi bi-trash"></i>
                            </button>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- 添加通知配置模态框 -->
    <div class="modal fade" id="addNotificationModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">添加通知配置</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="addNotificationForm">
                        <div class="mb-3">
                            <label class="form-label">名称</label>
                            <input type="text" class="form-control" name="name" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">类型</label>
                            <select class="form-select" name="type" id="notificationType" required onchange="toggleConfigFields()">
                                <option value="">选择类型</option>
                                <option value="email">Email</option>
                                <option value="dingtalk">钉钉</option>
                                <option value="feishu">飞书</option>
                                <option value="wechat">企业微信</option>
                            </select>
                        </div>
                        
                        <!-- Email 配置 -->
                        <div id="emailConfig" style="display: none;">
                            <div class="mb-3">
                                <label class="form-label">SMTP 服务器</label>
                                <input type="text" class="form-control" name="smtp_server">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">SMTP 端口</label>
                                <input type="number" class="form-control" name="smtp_port" value="587">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">发件人邮箱</label>
                                <input type="email" class="form-control" name="smtp_user">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">密码</label>
                                <input type="password" class="form-control" name="smtp_password">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">收件人邮箱</label>
                                <input type="email" class="form-control" name="to">
                            </div>
                        </div>
                        
                        <!-- 其他平台配置 -->
                        <div id="webhookConfig" style="display: none;">
                            <div class="mb-3">
                                <label class="form-label">Webhook URL</label>
                                <input type="url" class="form-control" name="webhook_url">
                            </div>
                        </div>
                        
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" name="enabled" checked>
                            <label class="form-check-label">启用</label>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="addNotification()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script src="/static/bootstrap.bundle.min.js"></script>
    <script>
        function toggleConfigFields() {
            const type = document.getElementById('notificationType').value;
            document.getElementById('emailConfig').style.display = type === 'email' ? 'block' : 'none';
            document.getElementById('webhookConfig').style.display = ['dingtalk', 'feishu', 'wechat'].includes(type) ? 'block' : 'none';
        }
        
        async function addNotification() {
            const form = document.getElementById('addNotificationForm');
            const formData = new FormData(form);
            const type = formData.get('type');
            
            let config = {};
            if (type === 'email') {
                config = {
                    smtp_server: formData.get('smtp_server'),
                    smtp_port: parseInt(formData.get('smtp_port')),
                    smtp_user: formData.get('smtp_user'),
                    smtp_password: formData.get('smtp_password'),
                    to: formData.get('to')
                };
            } else if (['dingtalk', 'feishu', 'wechat'].includes(type)) {
                config = {
                    webhook_url: formData.get('webhook_url')
                };
            }
            
            const data = {
                name: formData.get('name'),
                type: type,
                config: config,
                enabled: formData.get('enabled') === 'on'
            };
            
            try {
                const response = await fetch('/api/notification_configs/', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });
                
                if (response.ok) {
                    location.reload();
                } else {
                    alert('添加通知配置失败');
                }
            } catch (error) {
                console.error('Error:', error);
                alert('添加通知配置失败');
            }
        }
        
        function editNotification(id) {
            alert('编辑功能待实现 - ID: ' + id);
        }
        
        async function deleteNotification(id) {
            if (confirm('确定要删除这个通知配置吗？')) {
                try {
                    const response = await fetch(`/api/notification_configs/${id}`, {
                        method: 'DELETE'
                    });
                    
                    if (response.ok) {
                        location.reload();
                    } else {
                        alert('删除通知配置失败');
                    }
                } catch (error) {
                    console.error('Error:', error);
                    alert('删除通知配置失败');
                }
            }
        }
    </script>
</body>
</html>